import Url from 'url'
import Taro from '@tarojs/taro'
import GetBaseUrl from './baseUrl'

import RIsEmpty from 'ramda/src/isEmpty'
import RIsNil from 'ramda/src/isNil'

import { IReqApi } from '@/api/core'
import { IMethod, IResHeader } from '@/server/model'
import MsgTips from '@/utils/msgTips'

/**
 * 获取 请求方式 请求地址
 * @param api
 * @returns {method:IMethod,url:string} Object
 */
export function GetReqMethodAndPath(api: IReqApi): { method?: keyof IMethod; url: string } {
  const { method, prefix, version, path } = api
  const url = `${prefix && `/${prefix}`}${version && `/${version}`}/${path}`
  const reqUrl = GetBaseUrl(url)
  return { method, url: reqUrl }
}

/**
 * 获取 Token ==> (A/a)uthorization
 * @returns {authorization:string} obj
 */
export function GetStorageToken(): { authorization: string } {
  IsWxAuthorized()
  const Authorization = Taro.getStorageSync('authorization') || Taro.getStorageSync('Authorization')
  return { authorization: Authorization || '' }
}

/**
 * 判断用户是否授权
 * @return {boolean} boolean
 */
export function IsAuthorization(): boolean {
  const { authorization = '' } = GetStorageToken()
  // 用户是否 鉴权 无授权 前往授权界面
  if (!IsLoginPage() && (RIsEmpty(authorization) || RIsNil(authorization))) {
    // MsgTips('您还没有授权哦!')
    return false
    // NoAuthorizationWillGoToWxLogin()
  }
  return true
}

/**
 * 更新本地 Token ==> (A/a)uthorization
 * @param data IResHeader
 * @returns void
 */
export function UpdateStorageToken(data: IResHeader) {
  if (!data.Authorization && !data.authorization) return
  Taro.setStorageSync(data.authorization ? 'authorization' : 'Authorization', data['authorization'] || data['Authorization'])
}

/**
 * @description 获取当前页url
 * @returns string
 */
export const GetCurrentPageUrl: () => string = () => {
  const pages = Taro.getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const url = currentPage.route
  return url
}

/**
 * @description 是否微信授权登陆界面
 * @returns boolean
 */
export function IsLoginPage(): boolean {
  const path = GetCurrentPageUrl()
  return path.includes('Login')
}

/**
 * 无鉴权 情况 前往微信授权界面
 * @returns void
 */
export const NoAuthorizationWillGoToWxLogin = () => {
  MsgTips('授权失效,请前往前往个人中心进行授权')
  // if (!IsLoginPage()) {
  //   setTimeout(() => {
  //     Taro.navigateTo({
  //       url: '/pages/Login/index',
  //     })
  //   }, 1500)
  // }
  return
}

/**
 * 请求日志打印
 * @param {string} url 请求地址
 * @param {object} data 请求数据
 * @param {IMethod} method 请求方式
 * @return {void} void
 */
export const PrintReqLog = (url: string, data: any = {}, method?: keyof IMethod) => {
  const logText = `请求方式:${method} | 请求地址:${url} | 请求数据:${RIsEmpty(JSON.stringify(data)) || '无'}`
  console.log(logText)
}

/**
 * 响应日志打印
 * @param {String} url 请求地址
 * @param {Number} result 响应结果
 * @param {String} message 响应消息
 * @param {Array | Object} data 响应数据
 */
export const PrintResLog = (result: number, message: string, data: any = {}, url: string) => {
  const { protocol, host, path } = Url.parse(url)
  const logText = `响应地址:${protocol}${host}${path} | 响应结果:${result} | 响应消息:${message} | 响应数据:`
  console.log(logText)
  console.log(data || '无响应数据')
}

/**
 * 是否授权登陆
 */
function IsWxAuthorized() {
  // 可以通过 Taro.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
  // Taro.getSetting({
  // 	success: function (res) {
  // 		const SUI = res.authSetting['scope.userInfo']
  // 		if (SUI === undefined || SUI === false) {
  // 			MsgTips('未授权,即将前往授权界面!', { mask: true })
  // 			setTimeout(() => {
  // 				Taro.reLaunch({
  // 					url: '/pages/Login/index'
  // 				})
  // 			}, 1800)
  // 		}
  // 	}
  // })
}
